<?php

/**
 * role form base class.
 *
 * @method role getObject() Returns the current form's model object
 *
 * @package    forumCDE
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseroleForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                      => new sfWidgetFormInputHidden(),
      'code'                    => new sfWidgetFormInputText(),
      'description'             => new sfWidgetFormInputText(),
      'permissions_id'          => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('permissions'), 'add_empty' => true)),
      'activeStatus'            => new sfWidgetFormInputText(),
      'created_at'              => new sfWidgetFormDateTime(),
      'updated_at'              => new sfWidgetFormDateTime(),
      'reportdef_role_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'reportdef')),
      'documentation_role_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'documentation')),
      'messages_list'           => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'message')),
    ));

    $this->setValidators(array(
      'id'                      => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'code'                    => new sfValidatorString(array('max_length' => 50, 'required' => false)),
      'description'             => new sfValidatorString(array('max_length' => 50, 'required' => false)),
      'permissions_id'          => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('permissions'), 'required' => false)),
      'activeStatus'            => new sfValidatorInteger(),
      'created_at'              => new sfValidatorDateTime(),
      'updated_at'              => new sfValidatorDateTime(),
      'reportdef_role_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'reportdef', 'required' => false)),
      'documentation_role_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'documentation', 'required' => false)),
      'messages_list'           => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'message', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('role[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'role';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['reportdef_role_list']))
    {
      $this->setDefault('reportdef_role_list', $this->object->reportdef_role->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['documentation_role_list']))
    {
      $this->setDefault('documentation_role_list', $this->object->documentation_role->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['messages_list']))
    {
      $this->setDefault('messages_list', $this->object->messages->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->savereportdef_roleList($con);
    $this->savedocumentation_roleList($con);
    $this->savemessagesList($con);

    parent::doSave($con);
  }

  public function savereportdef_roleList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['reportdef_role_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->reportdef_role->getPrimaryKeys();
    $values = $this->getValue('reportdef_role_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('reportdef_role', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('reportdef_role', array_values($link));
    }
  }

  public function savedocumentation_roleList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['documentation_role_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->documentation_role->getPrimaryKeys();
    $values = $this->getValue('documentation_role_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('documentation_role', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('documentation_role', array_values($link));
    }
  }

  public function savemessagesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['messages_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->messages->getPrimaryKeys();
    $values = $this->getValue('messages_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('messages', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('messages', array_values($link));
    }
  }

}
